package day_2022_12_to_3.year2023;

import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @author haomin
 * @date 2023/03/30 20:58
 **/
public class Day3_30_Review {
    class Solution1 {
        public int[] spiralOrder(int[][] matrix) {
            if(matrix.length == 0 || matrix[0].length == 0) return new int[0];
            List<Integer> list = new LinkedList<>();
            int l = 0, r = matrix[0].length-1, u = 0, d = matrix.length-1;
            while(true) {
                for(int i = l; i <= r; ++i) list.add(matrix[u][i]);
                if(++u > d) break;

                for(int i = u; i <= d; ++i) list.add(matrix[i][r]);
                if(--r < l) break;

                for(int i = r; i >= l; --i) list.add(matrix[d][i]);
                if(--d < u) break;

                for(int i = d; i >= u; --i) list.add(matrix[i][l]);
                if(++l > r) break;
            }
            return list.stream().mapToInt(Integer::intValue).toArray();
        }
    }

    class Solution2 {
        public boolean validateStackSequences(int[] pushed, int[] popped) {
            Deque<Integer> s = new LinkedList<>();
            int i = 0, j = 0;
            while(j < pushed.length) {
                s.push(pushed[j]);
                j++;
                while(!s.isEmpty() && s.peek() == popped[i]) {
                    s.pop();
                    i++;
                }
            }
            return s.isEmpty();
        }
    }
}